package com.outsider.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;

public class AM {
	public static JSONObject getProductInfo(String url){
		JSONObject result=new JSONObject();
		OkHttp3 okHttp3=OkHttp3.getInstance();
		Map<String,String> header=new HashMap<>();
		header.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
		String html=okHttp3.getWithHeader(url, header);
		Pattern p=Pattern.compile("id=\"priceblock_ourprice\"(.*?)￥(.*?)\n");
		Matcher matcher=p.matcher(html);
		matcher.find();
        Double price=Double.parseDouble(matcher.group(2).replaceAll(",", ""));
        String code=url.split("/")[4];
        result.put("code", code);
		result.put("price", price);
		
		return result;
	}
	public static String getProductPic(String url){
		OkHttp3 okHttp3=OkHttp3.getInstance();
		Map<String,String> header=new HashMap<>();
		header.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
		String html=okHttp3.getWithHeader(url, header);
		Pattern p=Pattern.compile("var url = \"(.*?)\"");
		Matcher matcher=p.matcher(html);
		matcher.find();
		return matcher.group(1);
	}
    public static void main(String[] args){
    	System.out.println(AM.getProductInfo("http://www.amazon.cn/dp/B00NOQNHP2"));
    }
}
